<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 1.通过class类的静态函数实现
    // class SingleDog {
    //   static getInstance() {
    //     if(!SingleDog.instance) {
    //       SingleDog.instance = new SingleDog()
    //     }
    //     return SingleDog.instance
    //   }
    // }

    // let p1 = SingleDog.getInstance()
    // let p2 = SingleDog.getInstance()
    // console.log(p1 === p2);


    // 2.通过闭包实现
    function SingleDog() {
      // 构造函数逻辑
    }

    SingleDog.getInstance = (function () {
      let instance = null

      return function () {
        if (!instance) {
          instance = new SingleDog()
        }
        return instance
      }
    })()



  </script>
</body>

</html>